package com.zzyl.nursing.controller;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.huaweiyun.DeviceDto;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 智能设备基本信息Controller
 *
 * @author waker
 * @date 2025-09-16
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "智能设备基本信息相关接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询智能设备基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取智能设备基本信息列表")
    public TableDataInfo<List<Device>> list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 从物联网平台同步产品列表
     *
     * @return
     */
    @PostMapping("/syncProductList")
    @ApiOperation(value = "从物联网平台同步产品列表")
    public R syncProductList() {
        deviceService.syncProductList();
        return R.ok();
    }

    /**
     * 查询所有产品列表
     *
     * @return
     */
    @ApiOperation(value = "查询所有产品列表")
    @GetMapping("/allProduct")
    public R<List<ProductVo>> allProduct() {
        List<ProductVo> productVoList = deviceService.getAllProduct();
        return R.ok(productVoList);
    }

    /**
     * 注册设备
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "注册设备")
    @PostMapping("/register")
    public R registerDevice(@RequestBody DeviceDto dto) {
        deviceService.register(dto);
        return R.ok();
    }

    /**
     * 查询设备详细数据
     *
     * @param iotId
     * @return
     */
    @GetMapping("/{iotId}")
    @ApiOperation(value = "查询设备详细数据")
    public R<DeviceDetailVo> getDeviceDetail(@ApiParam(value = "设备id", required = true) @PathVariable String iotId) {
        DeviceDetailVo detailVo = deviceService.getDeviceDetail(iotId);
        return R.ok(detailVo);
    }

    /**
     * 查询设备上报的数据
     *
     * @param iotId
     * @return
     */
    @ApiOperation(value = "查询设备上报的数据")
    @GetMapping("/queryServiceProperties/{iotId}")
    public AjaxResult queryServiceProperties(@ApiParam(value = "设备id", required = true) @PathVariable String iotId) {
        return deviceService.queryServiceProperties(iotId);
    }

    /**
     * 修改设备信息
     * @param dto
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改设备信息")
    public AjaxResult updateDevice(@RequestBody DeviceDto dto){
        deviceService.updateDevice(dto);
        return AjaxResult.success();
    }

    /**
     * 删除设备
     * @param iotId
     * @return
     */
    @ApiOperation(value = "删除设备")
    @DeleteMapping("/{iotId}")
    public AjaxResult removeDevice(@ApiParam(value = "设备id", required = true) @PathVariable String iotId){
        deviceService.removeDevice(iotId);
        return AjaxResult.success();
    }

}
